import { Logger } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import * as fs from 'node:fs';
import * as path from 'node:path';
import { AppModule } from './app.module';

// console.log('🚀 ~ bootstrap ~ process.env.PORT:', process.env.PORT);

async function bootstrap() {
  const ssl = process.env.SSL === 'true' ? true : false;

  let httpsOptions = null;

  if (ssl) {
    const keyPath = process.env.SSL_SECRET_KEY || '';
    const certPath = process.env.SSL_CERTIFICATE || '';

    httpsOptions = {
      key: fs.readFileSync(path.join(__dirname, keyPath)),
      cert: fs.readFileSync(path.join(__dirname, certPath)),
    };
  }
  const app = await NestFactory.create(AppModule, {
    httpsOptions,
  });
  const port = Number(process.env.PORT) || 3080;
  const hostname = process.env.HOSTNAME || 'localhost';

  await app.listen(port, hostname, () => {
    const address = `https://${hostname}:${port}`;

    Logger.log(`Слушаем: ${address}`, 'Bootstrap');
  });
}

bootstrap();
